package com.iwomy.envpro.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.data.tenant.TenantContextHolder;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.dto.AirMonitorEquipmentDTO;
import com.iwomy.envpro.platform.dto.CommandCodeDTO;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.BasicEquipmentTypeEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.TemporalField;
import java.util.List;

/**
 * 监测设备管理
 *
 * @author pig
 * @date 2023-08-28 10:12:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/airMonitorEquipment")
@Tag(description = "airMonitorEquipment", name = "监测设备管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class AirMonitorEquipmentController {

	private final AirMonitorEquipmentService airMonitorEquipmentService;

	/**
	 * 分页查询
	 *
	 * @param page                   分页对象
	 * @param airMonitorEquipmentDTO 监测设备管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_view')")
	public R getAirMonitorEquipmentPage(@ParameterObject Page page, @ParameterObject AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.getAirMonitorEquipmentPage(page, airMonitorEquipmentDTO));
	}


	/**
	 * 通过id查询监测设备管理
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(airMonitorEquipmentService.getDetailById(id));
	}

	/**
	 * 新增监测设备管理
	 *
	 * @param airMonitorEquipment 监测设备管理
	 * @return R
	 */
	@Operation(summary = "新增监测设备管理", description = "新增监测设备管理")
	@SysLog("新增监测设备管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_add')")
	public R save(@RequestBody AirMonitorEquipmentEntity airMonitorEquipment) {


		return airMonitorEquipmentService.newlyAdded(airMonitorEquipment);
	}

	/**
	 * 修改监测设备管理
	 *
	 * @param airMonitorEquipment 监测设备管理
	 * @return R
	 */
	@Operation(summary = "修改监测设备管理", description = "修改监测设备管理")
	@SysLog("修改监测设备管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_edit')")
	public R updateById(@RequestBody AirMonitorEquipmentEntity airMonitorEquipment) {
		Long tenantId = TenantContextHolder.getTenantId();
		AirMonitorEquipmentEntity wrapperEntity = new AirMonitorEquipmentEntity();
		wrapperEntity.setEquipmentCode(airMonitorEquipment.getEquipmentCode());
		LambdaQueryWrapper<AirMonitorEquipmentEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		TenantContextHolder.clear();
		AirMonitorEquipmentEntity one = airMonitorEquipmentService.getOne(wrapper);
		TenantContextHolder.setTenantId(tenantId);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(airMonitorEquipment.getId())) {
			return R.failed("设备编号已存在");
		}

		return R.ok(airMonitorEquipmentService.updateById(airMonitorEquipment));
	}

	/**
	 * 通过id删除监测设备管理
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除监测设备管理", description = "通过id删除监测设备管理")
	@SysLog("通过id删除监测设备管理")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(airMonitorEquipmentService.remove(ids));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param airMonitorEquipment 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_airMonitorEquipment_export')")
	public List<AirMonitorEquipmentEntity> export(AirMonitorEquipmentEntity airMonitorEquipment) {
		return airMonitorEquipmentService.list(Wrappers.query(airMonitorEquipment));
	}

	/**
	 * @param airMonitorEquipment 查询条件
	 */
	@GetMapping("/list")
	public R list(AirMonitorEquipmentEntity airMonitorEquipment) {
		return R.ok(airMonitorEquipmentService.pageByScope(airMonitorEquipment));
	}

	/**
	 * @param airMonitorEquipmentDTO 查询条件
	 */
	@GetMapping("/listByTag")
	public R listByTag(AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.listByTag(airMonitorEquipmentDTO));
	}


	/**
	 * 获取指定区域下的所有设备
	 *
	 * @param page
	 * @param tag
	 * @return
	 */
	@GetMapping("/getAirMonitorEquipmentList")
	@PreAuthorize("@pms.hasPermission('platform_airSite_view')")
	public R getAirMonitorEquipmentList(Page page, @RequestParam("tag") Long tag) {
		return R.ok(airMonitorEquipmentService.getAirMonitorEquipmentList(page, tag));
	}

	/**
	 * 获取指定层级下的所有设备
	 *
	 * @param deptId
	 * @return
	 */
	@GetMapping("/getAirMonitorEquipmentListByDept/{deptId}")
	public R getAirMonitorEquipmentList( @PathVariable("deptId") Long deptId) {
		LambdaQueryWrapper<AirMonitorEquipmentEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(AirMonitorEquipmentEntity::getDeptId,deptId);
		return R.ok(airMonitorEquipmentService.list(queryWrapper));
	}


	/**
	 * 继电器控制
	 *
	 * @param airMonitorEquipment 继电器控制
	 * @return R
	 */
	@Operation(summary = "继电器控制", description = "继电器控制")
	@SysLog("继电器控制")
	@PostMapping("relayControl")
	public R relayControl(@RequestBody AirMonitorEquipmentEntity airMonitorEquipment) {
		ThreadUtil.sleep(15000);
		return R.failed("网络异常，请稍候再试");
	}

	/**
	 * 远程校准
	 *
	 * @param airMonitorEquipment 远程校准
	 * @return R
	 */
	@Operation(summary = "远程校准", description = "远程校准")
	@SysLog("远程校准")
	@PostMapping("remoteCalibration")
	public R remoteCalibration(@RequestBody AirMonitorEquipmentEntity airMonitorEquipment) {
		ThreadUtil.sleep(15000);
		return R.failed("网络异常，请稍候再试");
	}

	/**
	 * 进入配置
	 *
	 * @param commandCodeDTO 进入配置
	 * @return R
	 */
	@Operation(summary = "进入配置", description = "进入配置")
	@SysLog("进入配置")
	@PostMapping("config")
	public R config(@RequestBody CommandCodeDTO commandCodeDTO) {

		return R.ok(airMonitorEquipmentService.config(commandCodeDTO));
	}

	/**
	 * AT命令配置
	 *
	 * @param commandCodeDTO AT命令配置
	 * @return R
	 */
	@Operation(summary = "AT命令配置", description = "AT命令配置")
	@SysLog("AT命令配置")
	@PostMapping("atCommand")
	public R atCommand(@RequestBody CommandCodeDTO commandCodeDTO) {
		airMonitorEquipmentService.atCommand(commandCodeDTO);
		return R.ok();
	}

	/**
	 * 重启设备
	 *
	 * @param airMonitorEquipment 重启设备
	 * @return R
	 */
	@Operation(summary = "重启设备", description = "重启设备")
	@SysLog("重启设备")
	@PostMapping("reboot")
	public R reboot(@RequestBody AirMonitorEquipmentEntity airMonitorEquipment) {
		ThreadUtil.sleep(15000);
		return R.failed("网络异常，请稍候再试");
	}

	/**
	 * 设备在线情况
	 *
	 * @return R
	 */
	@Operation(summary = "设备在线情况", description = "设备在线情况")
	@GetMapping("/onlineStatus")
	public R onlineStatus(@ParameterObject AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.onlineStatus(airMonitorEquipmentDTO));
	}

/*********************************APP接口*******************************************/
	/**
	 * 分页查询
	 *
	 * @param page
	 * @param airMonitorEquipmentDTO 监测设备管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/appAirMonitorEquipmentPage")
	public R appAirMonitorEquipmentPage(Page page, @ParameterObject AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.getAirMonitorEquipmentPage(page, airMonitorEquipmentDTO));
	}

	/**
	 * 设备在线情况
	 *
	 * @return R
	 */
	@Operation(summary = "设备在线情况", description = "设备在线情况")
	@GetMapping("/appOnlineStatus")
	public R appOnlineStatus(@ParameterObject AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.onlineStatus(airMonitorEquipmentDTO));
	}

	/**
	 * @param airMonitorEquipmentDTO 查询条件
	 */
	@GetMapping("/appListByTag")
	public R appListByTag(AirMonitorEquipmentDTO airMonitorEquipmentDTO) {
		return R.ok(airMonitorEquipmentService.listByTag(airMonitorEquipmentDTO));
	}


	/**
	 * 通过id查询监测设备管理
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/appGetById/{id}")
	public R appGetById(@PathVariable("id") Long id) {
		return R.ok(airMonitorEquipmentService.getDetailById(id));
	}


}